package com.dog.basics.admin.controller.common;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dog.basics.bean.dto.admin.common.AdminDogComFileInfoFromDto;
import com.dog.basics.bean.dto.admin.common.AdminSonDogComFileInfoFromDto;
import com.dog.basics.bean.vo.admin.common.AdminDogComFileInfoVo;
import com.dog.basics.bean.vo.api.common.ApiComFileInfoUploadVo;
import com.dog.basics.common.annotation.SysLog;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.common.util.R;
import com.dog.basics.security.annotation.Inner;
import com.dog.basics.service.common.IDogComFileInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @description: 公共文件信息
 * @author: He
 * @date: Created in 2021/2/22 16:39
 * @version: 1.0
 * @modified By:
 */
@RestController
@RequestMapping("/admin/dogcomfileinfo")
@AllArgsConstructor
@Api(value = "公共文件信息", tags = "公共文件信息")
public class AdminComFileInfoController {

    private final IDogComFileInfoService comFileInfoService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param obj  文件信息表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "admin_dogcomfileinfo_view")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('admin_dogcomfileinfo_view')")
    public R<IPage<AdminDogComFileInfoVo>> getDogComFileInfoPage(PageParam page, AdminDogComFileInfoFromDto obj) {
        return R.ok(comFileInfoService.getPageList(page, obj));
    }

    /**
     * 子页面分页查询
     *
     * @param page 分页对象
     * @param obj  文件信息表
     * @return
     */
    @ApiOperation(value = "子页面分页查询", notes = "admin_dogcomfileinfo_sonPage")
    @GetMapping("/sonPage")
    @PreAuthorize("@pms.hasPermission('admin_dogcomfileinfo_sonPage')")
    public R<IPage<AdminDogComFileInfoVo>> sonPage(PageParam page, AdminSonDogComFileInfoFromDto obj) {
        return R.ok(comFileInfoService.getSonPageList(page, obj));
    }

    /**
     * 通过id查询文件信息表
     *
     * @param fileId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "admin_dogcomfileinfo_info")
    @GetMapping("/{fileId}")
    @PreAuthorize("@pms.hasPermission('admin_dogcomfileinfo_info')")
    public R<AdminDogComFileInfoVo> getById(@PathVariable("fileId") Long fileId) {
        return R.ok(comFileInfoService.getObjXq(fileId));
    }

    /**
     * @功能描述: 上传业务文件
     * @author He
     * @date 2021/2/22
     */
    @Inner
    @PostMapping("/upload/file/arr")
    @ApiOperation(value = "上传多文件信息", notes = "上传文件信息", tags = SwaggerConstants.PC_UPLOD)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件流", paramType = "List<MultipartFile>", required = true),
            @ApiImplicitParam(name = "aliasNames", value = "备注别名(可不传)[注意:如果要传必须和文件流数量保持一致,并且单备注名长度最大200]", paramType = "String[]")
    })
    @SneakyThrows
    public R<List<ApiComFileInfoUploadVo>> uploadArr(@RequestParam("file") List<MultipartFile> file, String[] aliasNames) {
        return R.ok(comFileInfoService.addApiComFileInfoUpload(file, CollUtil.newArrayList(aliasNames)));
    }

    /**
     * @功能描述: 单文件上传
     * @author He
     * @date 2021/2/24
     */
    @Inner
    @PostMapping("/upload/file")
    @ApiOperation(value = "上传单文件信息", notes = "上传文件信息", tags = SwaggerConstants.PC_UPLOD)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件流", paramType = "MultipartFile", required = true),
            @ApiImplicitParam(name = "aliasNames", value = "备注别名(可不传)", paramType = "String")
    })
    @SneakyThrows
    public R<ApiComFileInfoUploadVo> upload(@RequestParam("file") MultipartFile file, String aliasNames) {
        List<ApiComFileInfoUploadVo> uploadVos = comFileInfoService.addApiComFileInfoUpload(CollUtil.newArrayList(file), StrUtil.isNotBlank(aliasNames) ? CollUtil.newArrayList(aliasNames) : null);
        return R.ok(CollUtil.isNotEmpty(uploadVos) ? uploadVos.get(0) : null);
    }

    /**
     * 通过id删除文件信息表
     *
     * @param fileId id
     * @return R
     */
    @ApiOperation(value = "通过id删除文件信息表", notes = "admin_dogcomfileinfo_del")
    @SysLog("通过id删除文件信息表")
    @DeleteMapping("/{fileId}")
    @ApiImplicitParam(name = "fileId", value = "主键", required = true, dataType = "Long")
    @PreAuthorize("@pms.hasPermission('admin_dogcomfileinfo_del')")
    public R<Boolean> removeById(@PathVariable Long fileId) {
        return R.ok(comFileInfoService.deleteObj(fileId));
    }
}
